package com.vess.ratingweb;

import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class CallBack extends Activity{

	private Button btnBack;
	private Button btnBackLogin;
	private Button btnFinish;
	private String mStrUsername, mStrURL;
	private String mStrTimeStamp, mStrLogo, mStrCoupon;
	private int mIntUserId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.callback);
		
		btnBack = (Button)findViewById(R.id.btnBack);
		btnBackLogin = (Button)findViewById(R.id.btnBackLogin);
		btnFinish = (Button)findViewById(R.id.btnFinish);
		
		mStrURL = getIntent().getExtras().getString("url");
		mStrUsername = getIntent().getStringExtra("username");
		mStrTimeStamp = getIntent().getStringExtra("timestamp");
		mStrLogo = getIntent().getStringExtra("logo");
		mStrCoupon = getIntent().getStringExtra("coupon");
		mIntUserId = getIntent().getIntExtra("id", 0);
		
		new DownloadImageTask((ImageView) findViewById(R.id.imgCallBackLogo))
		.execute("http://reputation.bonothemes.com/user/php/uploads/" + mStrLogo);
		
		btnBack.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CallBack.this, Directory.class);
				intent.putExtra("username", mStrUsername);
				intent.putExtra("timestamp", mStrTimeStamp);
				intent.putExtra("logo", mStrLogo);
				intent.putExtra("coupon", mStrCoupon);
				intent.putExtra("id", mIntUserId);
				startActivity(intent);
			}
		});
		
		btnBackLogin.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CallBack.this, Login.class);
				intent.putExtra("username", mStrUsername);
				intent.putExtra("timestamp", mStrTimeStamp);
				intent.putExtra("logo", mStrLogo);
				intent.putExtra("coupon", mStrCoupon);
				intent.putExtra("id", mIntUserId);
				startActivity(intent);
			}
		});
		
		btnFinish.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CallBack.this, Result.class);
				intent.putExtra("url", mStrURL);
				intent.putExtra("timestamp", mStrTimeStamp);
				intent.putExtra("logo", mStrLogo);
				intent.putExtra("coupon", mStrCoupon);
				intent.putExtra("id", mIntUserId);
				startActivity(intent);
			}
		});
	}
	
	private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
		ImageView bmImage;

		public DownloadImageTask(ImageView bmImage) {
			this.bmImage = bmImage;
		}

		protected Bitmap doInBackground(String... urls) {
			String urldisplay = urls[0];
			Bitmap mIcon11 = null;
			try {
				InputStream in = new java.net.URL(urldisplay).openStream();
				mIcon11 = BitmapFactory.decodeStream(in);
			} catch (Exception e) {
				Log.e("Error", e.getMessage());
				e.printStackTrace();
			}
			return mIcon11;
		}

		protected void onPostExecute(Bitmap result) {
			bmImage.setImageBitmap(result);
		}
	}

	@Override
	public void onBackPressed() {
		Intent intent = new Intent(CallBack.this, Directory.class);
		intent.putExtra("username", mStrUsername);
		intent.putExtra("timestamp", mStrTimeStamp);
		intent.putExtra("logo", mStrLogo);
		intent.putExtra("coupon", mStrCoupon);
		intent.putExtra("id", mIntUserId);
		startActivity(intent);
		super.onBackPressed();
	}
	
}
